CLS

'2D curve fitting utility
'Replicates first order (linear) input data
'Linearly interpolate the antenna data

PRINT
INPUT "Enter disk letter for the system data (e.g., C)"; SysDataDrive$
PRINT
PRINT "INPUT THE FOLDER NAME (e.g.,Working\LA) "
PRINT "C:\ in front of folder name is implied. "
PRINT "Output files will appear in this folder."
INPUT TOPFOLDER$
PRINT
ROOTFOLDER$ = SysDataDrive$ + ":\" + TOPFOLDER$ + "\"
PRINT
PRINT "INPUT PATHS FILE NAME (e.g., FileList) "
PRINT ".CSV file name extension is implied. "
INPUT PATHSFILE$
PRINT

InputFileList$ = ROOTFOLDER$ + PATHSFILE$ + ".CSV"
OPEN InputFileList$ FOR INPUT AS #1

DO UNTIL EOF(1) = -1
    IF INKEY$ <> "" THEN GOTO 999
    INPUT #1, File$
    File$ = LTRIM$(RTRIM$(File$))
    FileSize& = LEN(File$)
    NameSize& = FileSize& - 5
    FileName$ = LEFT$(File$, NameSize&) + "B"
    NewFile$ = FileName$ + ".CSV"

    InputFile$ = ROOTFOLDER$ + "Raster.csv" 'list of X values WITHOUT a header (first line is an X value)
    OutputFile$ = ROOTFOLDER$ + NewFile$ 'curve-fitted data WITHOUT a header (output)
    DataFile$ = ROOTFOLDER$ + File$ 'existing list of X and Y values WITHOUT a header used to create the new interpelated curve (source of data)

    OPEN OutputFile$ FOR OUTPUT AS #4

    OPEN InputFile$ FOR INPUT AS #3
    DO UNTIL EOF(3) = -1
        IF INKEY$ <> "" THEN GOTO 999

        INPUT #3, X!
        X$ = LTRIM$(RTRIM$(STR$(X!)))

        GOSUB 2000 'interpolate the data
        GOSUB 3000

        PRINT #4, X$ + "," + Y$
        PRINT File$ + "," + X$ + "," + Y$

    LOOP '3

    CLOSE #3
    CLOSE #4

LOOP '1

999 CLOSE

END

'-------------------------------------------------

1000 'Linear curve-fit algorithm

A! = X2! - X1!
B! = X! - X1!
C! = Y2! - Y1!
Y! = Y1! + ((C! / A!) * B!)
 
RETURN
'------------------------------------

2000 'Interpolate the data

OPEN DataFile$ FOR INPUT AS #2
INPUT #2, X1!, Y1!
INPUT #2, X2!, Y2!

IF X! < X1! THEN
    X$ = LTRIM$(RTRIM$(STR$(X!)))
    PRINT "Value of X = <" + X$ + "> is below the range of input data"
    PRINT "program terminated"
    GOTO 999
END IF

IF EOF(2) = -1 THEN
    CLOSE #2
    GOSUB 1000
    GOTO 2100
END IF

IF X! <= X2! THEN
    CLOSE #2
    GOSUB 1000
    GOTO 2100
END IF

DO UNTIL EOF(2) = -1
    X1! = X2!
    Y1! = Y2!
    INPUT #2, X2!, Y2!

    IF EOF(2) = -1 THEN
        CLOSE #2
        GOSUB 1000
        GOTO 2100
    END IF

    IF X! <= X2! THEN
        CLOSE #2
        GOSUB 1000
        GOTO 2100
    END IF

LOOP
CLOSE #2

IF X! > X2! THEN
    X$ = LTRIM$(RTRIM$(STR$(X!)))
    PRINT "Value of X = <" + X$ + "> exceeds the range of input data"
    PRINT "program terminated"
    GOTO 999
END IF

GOSUB 1000

CLOSE #2

2100 RETURN
'------------------------------------
3000 'limit result to one decimal place

Y! = ABS(Y!)
Y& = INT(0.5 + (10. * Y!))
Y! = -1. * (Y& / 10.)
Y$ = LTRIM$(RTRIM$(STR$(Y!)))

RETURN
'------------------------------------

